#!/usr/bin/env node
const Scheduler = require('./node-scheduler').NodeScheduler;
const Request = require('./node-request').Request;
const fs = require('fs');
const Grabber = require('./node-grabber').Grabber;

//调度器
const sche = new Scheduler();
//读取来自命令行的指令
let order = process.argv.slice(2);
//将要运行的爬虫参数
let path = order[0];
//当前项目路径
let cur = process.cwd()+'/'+path+'/';

//读取项目配置
let config = null;
var pipeline = null;
if(fs.existsSync(cur+'config.json')) {
    console.log("开始加载配置...");
    config = fs.readFileSync(cur + 'config.json');
    console.log(config.toString());
    config = JSON.parse(config);
    //配置header
    Request.prototype.__proto__.headers = config.header;
    //配置cookie
    Request.prototype.__proto__.cookie = config.cookie_enable;
    //配置pipeline
    pipeline = require(cur + config.pipeline).Pipeline;
    // return;
    //配置下载器
    let time = config.downloader.time;
    let maxCount = config.downloader.maxCount;
    let retryCount = config.downloader.retryCount;
    let interval = config.interval;


    const Spider = require(cur + path + '-spider').Spider;
//运行爬虫
    var downloader = new Grabber({time,maxCount,retryCount,interval});

    var spider = new Spider();
    pipeline = new pipeline();
//爬虫以开启
    pipeline.start();
    sche.setDownloader(downloader);
    sche.setPipeline(pipeline);
    sche.run(spider);
//爬虫已退出
    process.on('exit', () => pipeline.close());
}else{
    console.log("没有找到配置文件config.json");
}